package com.mobile.learning;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.AbstractAction;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.mobile.learning.lib.DBClass;
import com.mobile.learning.lib.Login;
import com.mobile.learning.lib.ServiceHelper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private String id, pwd;
	DBClass db = new DBClass(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		if (db.isLogin()) {
			setContentView(R.layout.activity_profile);

			ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar_post);
			actionBar.setTitle("Profil");

			actionBar.setHomeAction(new IntentAction(this, MainMenuActivity
					.createIntent(this), R.drawable.ic_action_back));

			if (Build.VERSION.SDK_INT >= 11)
				actionBar.addAction(new MenuAction());

			TextView txtNI = (TextView) findViewById(R.id.txtNI);
			TextView txtNama = (TextView) findViewById(R.id.txtNama);
			TextView txtLog = (TextView) findViewById(R.id.txtLog);

			Login login = db.getLogin();

			txtNI.setText(login.getNomor_induk());
			txtNama.setText(login.getNama());
			txtLog.setText(login.getLogdate());

		} else {
			setContentView(R.layout.activity_login);

			Button btnLogin = (Button) findViewById(R.id.btnLogin);
			final EditText txtNI = (EditText) findViewById(R.id.inputNI);
			final EditText txtPwd = (EditText) findViewById(R.id.inputPass);

			ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar_post);
			actionBar.setTitle("Login");

			actionBar.setHomeAction(new IntentAction(this, MainMenuActivity
					.createIntent(this), R.drawable.ic_action_back));

			btnLogin.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub

					id = txtNI.getText().toString();
					pwd = txtPwd.getText().toString();
					new LoginTask().execute();

				}
			});
		}
	}

	// Menu
	private class MenuAction extends AbstractAction {

		public MenuAction() {
			super(R.drawable.ic_action_overflow);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void performAction(View view) {
			// TODO Auto-generated method stub
			PopupMenu popup = new PopupMenu(getApplicationContext(), view);
			MenuInflater inflater = popup.getMenuInflater();
			popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

				@Override
				public boolean onMenuItemClick(MenuItem item) {
					// TODO Auto-generated method stub
					switch (item.getItemId()) {
					case R.id.logout:
						Toast.makeText(LoginActivity.this,
								"Anda berhasil Logout !", Toast.LENGTH_SHORT)
								.show();
						Intent i = new Intent(getApplicationContext(),
								MainMenuActivity.class);
						startActivity(i);
						db.deleteSiswa();
						return true;
					default:
						return false;
					}

				}
			});
			inflater.inflate(R.menu.activity_login, popup.getMenu());
			popup.show();
		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.logout:
			Toast.makeText(LoginActivity.this, "Anda berhasil Logout !",
					Toast.LENGTH_SHORT).show();
			Intent i = new Intent(getApplicationContext(),
					MainMenuActivity.class);
			startActivity(i);
			db.deleteSiswa();
			return true;
		default:
			return false;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		if (db.isLogin()) {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.activity_login, menu);
		}
		return true;
	}

	// Login Task
	public class LoginTask extends AsyncTask<String, Void, Boolean> {

		private ProgressDialog dialog = new ProgressDialog(LoginActivity.this);

		protected void onPreExecute() {
			dialog.setMessage("Login as " + id + "..");
			dialog.show();
		}

		protected void onPostExecute(Boolean result) {
			dialog.dismiss();
			if (result == true) {
				Toast.makeText(LoginActivity.this, "Login Success",
						Toast.LENGTH_LONG).show();
				finish();

			} else {
				Toast.makeText(LoginActivity.this, "Login Failed",
						Toast.LENGTH_LONG).show();
			}

		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			ServiceHelper srv = new ServiceHelper();

			if (srv.login(LoginActivity.this, id, pwd)) {
				return true;
			} else {
				return false;
			}
		}

	}

}
